<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="地区名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：北京"></el-input>
      </el-form-item>
      <el-form-item label="地区拼音" prop="py_name">
        <el-input class="w500" v-model="form.py_name" placeholder="例如：beijing"></el-input>
      </el-form-item>
      <el-form-item label="邮政编码" prop="zipcode">
        <el-input class="w500" v-model="form.zipcode" placeholder="例如：100000"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input class="w100" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        rules: {
          name: [
            {required: true, message: '请输入地区名称', trigger: 'blur'}
          ],
          py_name: [
            {required: true, message: '请输入地区名称拼音', trigger: 'blur'}
          ],
          zipcode: [
            {required: true, message: '请输入地区邮政编码', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapState({
        form: state => state.address.saveData
      })
    },
    methods: {
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('address/addressEdit', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/address/list')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
